<?php
class Cache {
    public static function get($key) {
        //读取文件./cache
        $file = __DIR__ . '/../cache/' . hash("sha256",$key) . '.cache';
        if (file_exists($file)) {
            $result = json_decode(file_get_contents($file),true);
            if ($result['time'] > time()) {
                return $result;
            }
        } else {
            return false;
        }
    }
    public static function set($key, $value, $time = 3600) {
        //写入文件./cache
        $cacheDir = __DIR__ . '/../cache/';
        if (!file_exists($cacheDir)) {
            mkdir($cacheDir, 0777, true);
        }
        $file = $cacheDir . hash("sha256", $key) . '.cache';
        $data = array(
            'time' => time() + $time,
            'value' => $value
        );
        file_put_contents($file, json_encode($data));
    }
}